package com.example.fw;

import java.util.List;
import java.util.Random;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

import com.example.tests.ContactData;
import com.example.utils.SortedListOf;


public class ContactHelper extends HelperBase {

	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}
	
	private SortedListOf<ContactData> cachedContacts;
	
	public SortedListOf<ContactData> getContacts() {
		if (cachedContacts == null) {
			rebuildCache();
		}
		return cachedContacts;
	}
	
	
	private void rebuildCache() {
		cachedContacts = new SortedListOf<ContactData>();
		
		manager.navigateTo().mainPage();
		List<WebElement> rows = driver.findElements(By.xpath("//tr[@name='entry']"));
		for (WebElement row : rows) {
			String firstname = row.findElements(By.tagName("td")).get(2).getText();
			String lastname = row.findElements(By.tagName("td")).get(1).getText();
			cachedContacts.add(new ContactData().withFirstname(firstname).withLastname(lastname));
		}
	}

	public ContactHelper createContact(ContactData contact) {
		initContactCreation();
		fillContactForm(contact, CREATION);
		submitContactCreation();
		returnToHomePage();
		rebuildCache();
		return this;
	}
	
	public ContactHelper modifyContact(ContactData contact, int index) {
		initContactModification(index);
		fillContactForm(contact, MODIFICATION);
		submitContactModification();
		returnToHomePage();
		rebuildCache();
		return this;
	}
	
	public ContactHelper deleteContact(int index) {
		selectContactByIndex(index);
		submitContactDeletion();
		rebuildCache();
		return this;
	}

//-----------------------------------------------------------------
	
	public ContactHelper returnToHomePage() {
		click(By.linkText("home page"));
		return this;
	}
	
	
	public ContactHelper initContactCreation() {
		click(By.linkText("add new"));
		return this;
	}
	

	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		type(By.name("firstname"), contact.getFirstname());
		type(By.name("lastname"), contact.getLastname());
	    type(By.name("address"), contact.getAddress1());
	    type(By.name("home"), contact.getHomephone());
	    type(By.name("mobile"), contact.getMobilephone());
	    type(By.name("work"), contact.getWorkphone());
	    type(By.name("email"), contact.getEmail1());
	    type(By.name("email2"), contact.getEmail2());
	    contact.withBday(selectRandomItem("bday"));
	    contact.withBmonth(selectRandomItem("bmonth"));
	    type(By.name("byear"), contact.getByear());
	    if (formType == CREATION){
			contact.withContactgroup(selectRandomItem("new_group"));
	    } else {
	    	if (driver.findElements(By.name("new_group")).size() != 0) {
	    		throw new Error("Group selector exists in contact modification form");
	    	}
	    }
	    type(By.name("address2"), contact.getAddress2());
	    type(By.name("phone2"), contact.getPhone2());
		return this;
	}
	
	
	public String selectRandomItem (String type) {
		Select dropdown = new Select(chooseDropDown(type));
		Random rnd = new Random();
		int listSize = dropdown.getOptions().size();
		int randomIndex = rnd.nextInt(listSize);
		dropdown.selectByIndex(randomIndex);
		return dropdown.getAllSelectedOptions().get(0).getText();
	}
	
	
	public WebElement chooseDropDown (String type) {
		WebElement selectElem;
		if (type == "bday") {
			selectElem = driver.findElement(By.name("bday"));
		} else {
			if (type == "bmonth") {
				selectElem = driver.findElement(By.name("bmonth"));
			} else { 
				selectElem = driver.findElement(By.name("new_group"));
				}
			}
			return selectElem;
		}


	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		cachedContacts = null;
		return this;
	}

	public ContactHelper initContactModification(int index) {
		selectContactByIndex(index);
		return this;
	}
	
	
	private ContactHelper selectContactByIndex(int index) {
		index = index + 2;
		click(By.xpath("//tr[" + index + "]/td[7]/a/img"));
		return this;
	}
	
	public ContactHelper submitContactModification() {
		click(By.xpath("//form/input[11]"));
		cachedContacts = null;
		return this;
	}


	private void submitContactDeletion() {
		click(By.xpath("//form[2]/input[2]"));
		cachedContacts = null;
	}

}
